# -*- coding: utf-8 -*-

"""
DateTime   : 2020/12/17 17:29
Author     : ZhangYafei
Description: 装饰器作用在类上
"""


# 下面是一个重写了特殊方法 __getattribute__ 的类装饰器，可以打印日志:
def log_getattribute(cls):
    origin_getattribute = cls.__getattribute__

    def new_getattribute(self, name):
        print('greeting:', name)
        return origin_getattribute(self, name)

    cls.__getattribute__ = new_getattribute
    return cls


# 应用
@log_getattribute
class A:
    def __init__(self, x):
        self.x = x

    def spam(self):
        pass


if __name__ == '__main__':
    a = A('x')
    print(a.x)
    print(a.spam())
    """
    greeting: x
    x
    greeting: spam
    None
    """
